package co.yixiang.entity

import co.yixiang.util.MBaseTable
import co.yixiang.util.nextId
import com.fasterxml.jackson.annotation.JsonFormat
import io.swagger.annotations.ApiModel
import io.swagger.annotations.ApiModelProperty
import me.liuwj.ktorm.dsl.QueryRowSet
import me.liuwj.ktorm.schema.int
import me.liuwj.ktorm.schema.long
import me.liuwj.ktorm.schema.text
import me.liuwj.ktorm.schema.varchar
import org.springframework.data.mongodb.core.index.Indexed
import org.springframework.data.mongodb.core.mapping.Document
import org.springframework.data.mongodb.core.mapping.Field
import org.springframework.data.mongodb.repository.ReactiveMongoRepository
import reactor.core.publisher.Flux
import reactor.core.publisher.Mono
import java.time.LocalDateTime
import javax.persistence.*

/*----------------------------帖子模块----------------------------*/


object Post : MBaseTable<Posts>("fly_post",null) {

    val content = text("content")
    val pics = text("pics")
    var longitude = varchar("longitude")
    var latitude = varchar("latitude")

    override fun doCreateEntity(row: QueryRowSet, withReferences: Boolean): Posts {

        return Posts(
            content = row[content],
            pics = row[pics],
            longitude = row[longitude],
            latitude = row[latitude],

            id = row[id],
            createTime = row[createTime]!!,
            updateTime = row[updateTime],
            createBy = row[createBy]!!,
            deleted = row[deleted]!!
        )
    }
}


@ApiModel("帖子实体")
@Entity
@org.hibernate.annotations.Table(appliesTo = "fly_post", comment = "帖子")
@Table(name = "fly_post", indexes = [Index(columnList = "create_time")])
data class Posts(

    @ApiModelProperty("发布内容")
    @Column(columnDefinition = "text(10) comment '发布内容'")
    var content: String? = "",

    @ApiModelProperty("发布图片")
    @Column(columnDefinition = "longtext comment '发布图片 , 拼接'")
    var pics: String? = "",

    @ApiModelProperty("经度")
    @Column(columnDefinition = "varchar(100) default '' comment '经度'")
    var longitude: String? = "",

    @ApiModelProperty("维度")
    @Column(columnDefinition = "varchar(100) default '' comment '维度'")
    var latitude: String? = "",


    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @ApiModelProperty("id")
    var id: Long? = null,

    @Column(columnDefinition = "bigint(11) comment '创建者'")
    @ApiModelProperty("创建者")
    var createBy: Long? = null,

    @Column(name = "create_time", nullable = false)
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @ApiModelProperty("创建时间")
    var createTime: LocalDateTime = LocalDateTime.now(),

    @Column(name = "update_time", nullable = true)
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @ApiModelProperty("更新时间")
    var updateTime: LocalDateTime? = LocalDateTime.now(),

    @Column(name = "deleted", nullable = false)
    @ApiModelProperty("逻辑删除")
    var deleted: Boolean = false
)


/*----------------------------评论模块----------------------------*/

open class Comment(alias: String?) : MBaseTable<Comments>("fly_comment", alias) {

    companion object : Comment(null)
    override fun aliased(alias: String) = Comment(alias)

    val content = varchar("content")
    val pid = long("pid")
    val postId = long("post_id")
    val type = int("type")

    override fun doCreateEntity(row: QueryRowSet, withReferences: Boolean): Comments {

        return Comments(
            content = row[content],
            pid = row[pid],
            postId = row[postId],
            type = row[type],

            id = row[id],
            createTime = row[createTime]!!,
            updateTime = row[updateTime],
            createBy = row[createBy]!!,
            deleted = row[deleted]!!
        )
    }
}


@ApiModel("评论实体")
@Entity
@Table(name = "fly_comment", indexes = [Index(columnList = "create_time")])
@org.hibernate.annotations.Table(appliesTo = "fly_comment", comment = "评论")
data class Comments(

    @Column(columnDefinition = "varchar(100) default '' comment '评论内容'")
    @ApiModelProperty("评论内容")
    var content: String? = "",

    @Column(columnDefinition = "bigint(11) comment '上级id'")
    @ApiModelProperty("上级id")
    var pid: Long? = 0L,

    @Column(columnDefinition = "bigint(11) comment '帖子id'")
    @ApiModelProperty("帖子id")
    var postId: Long? = null,

    @Column(columnDefinition = "tinyint(11) default 0 comment '评论类型 0 帖子'")
    @ApiModelProperty("评论类型")
    var type: Int? = 0,

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @ApiModelProperty("id")
    var id: Long? = null,

    @Column(columnDefinition = "bigint(11) comment '创建者'")
    @ApiModelProperty("创建者")
    var createBy: Long? = null,

    @Column(name = "create_time", nullable = false)
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @ApiModelProperty("创建时间")
    var createTime: LocalDateTime = LocalDateTime.now(),

    @Column(name = "update_time", nullable = true)
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @ApiModelProperty("更新时间")
    var updateTime: LocalDateTime? = LocalDateTime.now(),

    @Column(name = "deleted", nullable = false)
    @ApiModelProperty("逻辑删除")
    var deleted: Boolean = false
)


/*----------------------------关注模块----------------------------*/

interface AttentionRepository: ReactiveMongoRepository<Attention, String> {

    fun getAttentionsByUserIdOrderByCreateTimeDesc(userId: Long?): Flux<Attention>

    fun getAttentionsByAttentionUserIdOrderByCreateTimeDesc(userId: Long?): Flux<Attention>
}


@Document("attention")
@ApiModel("我的关注")
data class Attention(

    @org.springframework.data.annotation.Id
    var id: String? = nextId(),

    @ApiModelProperty("用户id")
    @Field("user_id")
    @Indexed
    var userId: Long? = null,

    @ApiModelProperty("关注的人的id")
    @Field("attention_userId")
    var attentionUserId: Long? = null,

    @ApiModelProperty("创建时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @Field("create_time")
    var createTime: LocalDateTime? = LocalDateTime.now()
)

/*----------------------------点赞模块----------------------------*/

interface LikeRepository : ReactiveMongoRepository<Like, String> {

    /**
     * 通过用户id查询该用户的关注列表
     */
    fun getLikesByUserIdOrderByCreateTimeDesc(userId: Long?): Flux<Like>

    /**
     * 查询用户是否已经点赞
     */
    fun getLikeByLikeIdAndUserId(likeId: Long?, userId: Long? ): Flux<Like>

    /**
     * 获取帖子的点赞个数
     */
    fun getLikesByLikeId(likeId: Long?): Flux<Like>
}

@Document("like")
@ApiModel("我的点赞")
data class Like(

    @org.springframework.data.annotation.Id
    var id: String? = nextId(),

    @ApiModelProperty("用户id")
    @Field("user_id")
    @Indexed
    var userId: Long? = null,

    @ApiModelProperty("点赞id")
    @Field("like_id")
    var likeId: Long? = null,

    @ApiModelProperty("点赞类型 0 默认帖子")
    var type: Int? = 0,

    @ApiModelProperty("创建时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @Field("create_time")
    var createTime: LocalDateTime? = LocalDateTime.now()
)



interface VisitRepository: ReactiveMongoRepository<Visit, Long> {

    /**
     * 获取我的来访者
     */
    fun getVisitsByUserIdOrderByCreateTimeDesc(userId: Long?): Flux<Visit>

    /**
     * 通过来访者和被来访者获取信息
     */
    fun getVisitByUserIdAndVisitUserId(userId: Long?, visitUserId: Long?): Mono<Visit>

}

@Document("visit")
@ApiModel("来访者")
data class Visit(

    @org.springframework.data.annotation.Id
    var id: String? = nextId(),

    @ApiModelProperty("被访问的用户id")
    @Field("user_id")
    var userId: Long? = null,

    @ApiModelProperty("访问者用户id")
    @Field("visit_user_id")
    var visitUserId: Long? = null,

    @ApiModelProperty("创建时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @Field("create_time")
    var createTime: LocalDateTime? = LocalDateTime.now()
)


